/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quan;

import java.awt.Graphics;
import java.awt.Point;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Quannk
 */
public class QLine {

    QInternalFrame f1, f2;
    Vector<Point> points = new Vector<Point>();

    public void addPoint(Point p) {
        points.addElement(p);
    }

    public QLine(QInternalFrame internalFrame1, QInternalFrame internalFrame2) {
        f1 = internalFrame1;
        f2 = internalFrame2;
        points.add(f1.getSelectedPoint());
        points.add(f2.getSelectedPoint());
    }

    public boolean containFrame(QInternalFrame frame) {
        if (frame == f1 || frame == f2) {
            return true;
        }
        return false;
    }

    public void drawLine(Graphics g) {
        for (int i = 0; i < points.size() - 1; i++) {
            int x1, y1, x2, y2;
            x1 = points.get(i).x;
            y1 = points.get(i).y;
            x2 = points.get(i + 1).x;
            y2 = points.get(i + 1).y;
            if (i == 0) {
                g.drawOval(x1 - 4, y1 - 4, 8, 8);
            } else {
                g.drawRect(x1 - 2, y1 - 2, 4, 4);
            }
            if (i + 1 == points.size() - 1) {
                g.drawRect(x2 - 4, y2 - 4, 8, 8);
            } else {
                g.drawRect(x2 - 2, y2 - 2, 4, 4);
            }

            g.drawLine(x1, y1, x2, y2);
        }
    }

    public void updatePoint(QInternalFrame frame) {
        if (frame == f1) {
            points.set(0, frame.getSelectedPoint());
        } else {
            points.set(points.size() - 1, frame.getSelectedPoint());
        }
    }

    public boolean addFoldPoint(Point p) {
        boolean isValidPoint = false;
        for (int i = 0; i < points.size() - 1; i++) {
            isValidPoint = QUtility.checkSelectedPoint(points.get(i), points.get(i + 1), p);
            if (isValidPoint) {
                points.insertElementAt(p, i + 1);
                return true;
            }
        }
        return false;
    }

    public Point getFoldPoint(Point p) {
        for (int i = 1; i < points.size() - 1; i++) {
            if (QUtility.comparePoint(p, points.get(i))) {
                return points.get(i);
            }
        }
        return null;
    }

    public void removeAllFoldPoint() {
        if (points.size() > 2) {
            points.subList(1, points.size() - 1).clear();
        }
    }
}
